package org.blog.service;

import java.util.List;

import org.blog.dao.ArticleDao;
import org.blog.helper.Constants;
import org.blog.helper.Result;
import org.blog.model.ActionType;
import org.blog.model.ModelArticle;
import org.blog.model.ModelPassport;
import org.blog.service.util.ServiceConstants;
import org.blog.service.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;

public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private ArticleDao articleDao;

	public Result<?> addUserArticleWithRole(ModelArticle t,
			ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ARTICLE) {
			if (ValidateHelper.validate(t, ActionType.MODIFY)) {
				t.setUsername(passport.getUsername());
				t.setAction(1);
				t.setActionTime(System.currentTimeMillis() / 1000);
				t.setCreateTime(System.currentTimeMillis() / 1000);
				articleDao.addUserArticle(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	public Result<?> modifyUserArticleWithRole(ModelArticle t,
			ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ARTICLE) {
			if (ValidateHelper.validate(t, ActionType.MODIFY)) {
				t.setUsername(passport.getUsername());
				t.setAction(2);
				t.setActionTime(System.currentTimeMillis() / 1000);
				articleDao.modifyUserArticle(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	public Result<?> deleteUserArticleWithRole(ModelArticle t,
			ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ARTICLE) {
			if (ValidateHelper.validate(t, ActionType.DELETE)) {
				t.setUsername(passport.getUsername());
				t.setAction(3);
				t.setActionTime(System.currentTimeMillis() / 1000);
				articleDao.deleteUserArticle(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	public Result<?> deleteAdminArticleWithRole(ModelArticle t,
			ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			if (ValidateHelper.validate(t, ActionType.DELETE)) {
				t.setAction(3);
				t.setActionTime(System.currentTimeMillis() / 1000);
				articleDao.deleteAdminArticle(t);
				return new Result<Object>(200);
			} else {
				return new Result<Object>(400);
			}
		} else {
			return new Result<Object>(401);
		}
	}

	@Override
	public Result<ModelArticle> getArticle(ModelArticle t) {
		if (ValidateHelper.validate(t, ActionType.GET)) {
			ModelArticle article = articleDao.getArticle(t.getId());
			return new Result<ModelArticle>(article, 200);
		} else {
			return new Result<ModelArticle>(400);
		}
	}

	@Override
	public Result<List<ModelArticle>> getUserListArticle(ModelPassport passport) {
		if (passport != null) {
			List<ModelArticle> list = articleDao.getUserListArticle(passport
					.getUsername());
			return new Result<List<ModelArticle>>(list, 200);
		} else {
			return new Result<List<ModelArticle>>(401);
		}
	}

	@Override
	public Result<List<ModelArticle>> getUserDraftListArticle(
			ModelPassport passport) {
		if (passport != null) {
			List<ModelArticle> list = articleDao
					.getUserDraftListArticle(passport.getUsername());
			return new Result<List<ModelArticle>>(list, 200);
		} else {
			return new Result<List<ModelArticle>>(401);
		}
	}

	public Result<?> getListArticle(int page) {
		int start = (page - 1) * Constants.pages;
		int end = page * Constants.pages;
		List<ModelArticle> list = articleDao.getListArticle(start, end);
		return new Result<List<ModelArticle>>(list, 200);
	}

	public Result<?> getListCatalogArticle(ModelArticle t, int page) {
		if (ValidateHelper.validate(t, ActionType.GET_PARENT)) {
			int start = (page - 1) * Constants.pages;
			int end = page * Constants.pages;
			List<ModelArticle> list = articleDao.getListCatalogArticle(
					t.getCatalogId(), start, end);
			return new Result<List<ModelArticle>>(list, 200);
		} else {
			return new Result<List<ModelArticle>>(400);
		}
	}

}
